/*
 * Copyright 2009 Brook Smith brook.smith@paradise.net.nz.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.javagenerate.generator.script.gen;

import org.javagenerate.generator.AccessorDefinition;
import org.javagenerate.generator.MethodDefinition;
import org.javagenerate.generator.Parameter;
import org.javagenerate.generator.script.ClassDef;
import org.javagenerate.generator.script.GenBeanDefinition;

public class GwtListDef extends ClassDef {

	private ClassDef interfaceDef = null;
	private ClassDef serviceAsync = null;

	public GwtListDef(String classname, String patternName) {
		super(classname, patternName);
	}

	@Override
	public void addBeanDefiniation(GenBeanDefinition beanDefinition) {
		// ignore for now
	}

	public ClassDef getInterfaceDef() {
		return interfaceDef;
	}


	private MethodDefinition getGetCell() {
		
		/**
		public Widget getCell(int row, int column) {
		if (column == 0) {
			Label label = new Label(data[row].getFirstname());
			return label;
		} else if (column == 1) {
			Label label = new Label(data[row].getLastname());
			return label;
		} else if (column == 2) {
			Label label = new Label(data[row].getId().toString());
			label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
			return label;
		}
		return null;
	}
		 */
		MethodDefinition definition = new MethodDefinition("Widget", "getCell");
		definition.addParameter(new Parameter("row", "int"));
		definition.addParameter(new Parameter("column", "int"));
		
		StringBuilder sb = new StringBuilder();
		int i = 0;
		for (AccessorDefinition accessorDefinition : interfaceDef.getBeanMethods()) {
			if (i == 0) {
				sb.append("if (column == 0) {\n");
			} else {
				sb.append("} else if (column == "+i+") {\n");
			}
			if (accessorDefinition.getType().equals("String")) {
				sb.append("return new Label(data[row]." + accessorDefinition.getAccessorName() + "());\n");
			} else {
				sb.append("if (data[row]." + accessorDefinition.getAccessorName() + "() == null) {\n");
				sb.append("return new Label();\n");
				sb.append("} else {\n");
				sb.append("return new Label(data[row]." + accessorDefinition.getAccessorName() + "().toString());\n}\n");
			}
			i++;
		}
		sb.append("}\nreturn null;\n");
		definition.setContent(sb.toString());
		return definition;
	}
	
	private MethodDefinition getGetLabels() {
		/*
		 * 
		public Widget[] getLabels() {
		if (labels == null) {
			List<Widget> tmpLabels = new ArrayList<Widget>(); 

			tmpLabels.add(new Label("Firstname"));
			tmpLabels.add(new Label("Lastname"));
			tmpLabels.add(new Label("id"));
			
			for (Widget widget : tmpLabels) {
				widget.addStyleDependentName("heading");
			}
			labels = tmpLabels.toArray(new Widget[]{});
		}

		return labels;
	}
		 */
		MethodDefinition definition = new MethodDefinition("Widget[]", "getLabels");
		
		StringBuilder sb = new StringBuilder("if (labels == null) {\nList<Widget> tmpLabels = new ArrayList<Widget>();\n");
		for (AccessorDefinition accessorDefinition : interfaceDef.getBeanMethods()) {
			sb.append("tmpLabels.add(new Label(\"" + accessorDefinition.getName() + "\"));\n");
		}
		sb.append("for (Widget widget : tmpLabels) {\nwidget.addStyleDependentName(\"heading\");\n}\nlabels = tmpLabels.toArray(new Widget[]{});\n}\nreturn labels;\n");
		definition.setContent(sb.toString());
		
		return definition;
	}
	
	@Override
	public void preWrite() {
		super.preWrite();
		getImplements().add("IListContent<" + interfaceDef.getClassName() + ", "+serviceAsync.getClassName() +">");
		addMethod(getGetCell());
		addMethod(getGetLabels());
	}


	public void setInterfaceDef(ClassDef interfaceDef) {
		this.interfaceDef = interfaceDef;

		getImports().add(interfaceDef.getPackageName() + "." + interfaceDef.getClassName());

	}
	
	public void setServiceAsync(ClassDef serviceAsync) {
		this.serviceAsync = serviceAsync;

		getImports().add(serviceAsync.getPackageName() + "." + serviceAsync.getClassName());

	}
	
}
